package com.dmsdbj.itoo.service.impl;

import com.dmsdbj.itoo.entity.TbUser;
import com.dmsdbj.itoo.dao.TbUseDao;
import com.dmsdbj.itoo.exception.ItooException;
import com.dmsdbj.itoo.exception.OnlineExamException;
import com.dmsdbj.itoo.repository.TbUseRepository;
import com.dmsdbj.itoo.service.TbUseService;
import com.dmsdbj.itoo.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
// @CacheConfig(cacheNames = "test1") 如果添加此注解，以下方法可省略cacheNames = "test1"配置项
public class TbUseServiceImpl implements TbUseService {

    @Autowired
    TbUseRepository repository;

    @Autowired
    TbUseDao tbUseDao;

    private RedisUtils redisUtils;

    @Override
    public TbUser findOne(long id) {
        return repository.findOne(id);
    }

    // @CachePut(cacheNames = "test1",key = "123")  每次都会执行方法，并放入缓存(更新)
    // @Cacheable(cacheNames = "test1",key = "123")  缓存中有数据的时候不执行  （key不写默认是方法的参数），可添加condition，按条件缓存，添加unless，只缓存正确结果
    // @CacheEvict(cacheNames = "test1",key = "123") 执行方法之后删除redis中的记录
    //注意以上标签缓存的对象要实现序列化
    @Override
    public int findUserCount() {
        int userCount = tbUseDao.findUserCount();

        if (userCount > 0) {

            throw new OnlineExamException(0000,"我是online异常");

        }
        return userCount;

    }


}
